﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;
using System.ComponentModel;

namespace Pingy
{
    public class PingCommand : INotifyPropertyChanged
    {
		private IPAddress address;
		public IPAddress Address
		{
			get { return address; }
			set { address = value; SendPropertyChangedEvent("Address"); }
		}

        private string status;
        public string Status
        {
            get { return status; }
            set { status = value; SendPropertyChangedEvent("Status"); }
        }

        private PingReply reply;
        public PingReply Reply
        {
            get { return reply; }
            set { reply = value; SendPropertyChangedEvent("Reply"); }
        }

        private string hostInfo;
		public string HostInfo
        {
            get { return hostInfo; }
            set { hostInfo = value; SendPropertyChangedEvent("HostInfo"); }
        }

        public bool HasReply
        {
            get { return Reply != null && Reply.Status == IPStatus.Success; }
        }

        public PingCommand(IPAddress address)
        {
            Address = address;
        }

		public PingCommand(string hostName)
		{
			HostInfo = hostName;
		}

        public override string ToString()
        {
			return GetHostnameOrAddress();
        }

		public string GetHostnameOrAddress()
		{
			return HostInfo ?? Address.ToString();
		}

        public event PropertyChangedEventHandler PropertyChanged;

        private void SendPropertyChangedEvent(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
